/**
 * Created by ali on 2017/12/4.
 */

var flag; //设置添加或修改的标识
$(function() {
    loadData(datalist.browLog);
});

//加载数据
function loadData(data) {
    $('#tabelData').datagrid('loadData', data);
    $('#tabelData').datagrid({
        //设置只能选中单行
        onClickRow: function(rowIndex, rowData) {
            $('#tabelData').datagrid('unselectAll');
            $('#tabelData').datagrid('selectRow', rowIndex);
        },
        //双击事件
        onDblClickRow: function(rowIndex, rowData) {
            $('#tabelData').datagrid('unselectAll');
            $('#tabelData').datagrid('selectRow', rowIndex);
            detail(rowIndex);
        }
    });
}
 
//排序
function numberSort(a, b) {
    var number1 = parseFloat(a);
    var number2 = parseFloat(b);
    return(number1 > number2 ? 1 : -1);
}

//保存
function save() {
    getRowsId();
    //从本地得到数据
    var list = getLocalStorage();
    //创建一个新的json对象
    var newUser = {};

    newUser.id = $("#id").val();
    newUser.name = $("#name").val();
    newUser.gender = $("#gender").val();
    newUser.eMail = $("#eMail").val();
    newUser.phone = $("#phone").val();
    newUser.address = $("#address").val();
    newUser.employeeType = $("#employeeType").val();
    newUser.job = $("#job").val();

    if($("#fr").form("validate")) {
        //判断是添加还是修改
        if(flag) {
            setLocalStorage(newUser);
        } else {
            $.each(list, function(i) {
                if(list[i].id == $("#id").val()) {
                    list.splice(i, 1, newUser);
                }
            });
            setLocalStorage(list);
        }
        //关闭模态框
        $("#dlg").dialog("close");
        //清空表单中的数据
        $("#fr").form("reset");
    }

    //加载数据
    $('#tabelData').datagrid('loadData', getLocalStorage());
}

//取消
function cancel() {
    $("#dlg").dialog("close");
    $("#detail_dlg").dialog("close");
    $("#fr").form("reset");
}

//删除
function remove() {
    var seletRows = $('#tabelData').datagrid('getSelections');
    var dataArr = getLocalStorage();

    console.log(dataArr);
    if(seletRows.length == 0) {
        alert("未选中 数据");
    } else {
        alert("确定删除这条数据？");
        for(var i = 0; i < dataArr.length; i++) {
            for(var j = 0; j < seletRows.length; j++) {
                if(dataArr[i].id == seletRows[j].id) {
                    dataArr.splice(i, 1);
                }
            }
        }
    }
    setLocalStorage(dataArr);
    //加载数据
    $('#tabelData').datagrid('loadData', getLocalStorage());
}

//在表格中添加 详情
function opration(value, row, index) {
    return "<a href='#' onclick='detail(" + index + ")'>详情</a>";
}
